/**
* @Title: uploadDepartmentModalCtrl_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-ZHANGJICHENG001
* @date 2014年12月29日 下午14:46:06
* @version V1.3
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('uploadDepartmentModalCtrl', ['$scope','$rootScope', '$http','BaseURL', 'Modal','$modalInstance','$upload','institutionDetailService', function($scope, $rootScope,$http, baseUrl, Modal,$modalInstance,$upload,institutionDetailService){
		
		var fileTypes = ['xls'];
		$scope.files = [];
		
		
		function isBelongToFileTypes(fileTypes, fileType){
			for(var i = 0; i < fileTypes.length; i++){
				var tmpFileType = fileTypes[i];
				if(tmpFileType == fileType){
					return true;
				}
			}
			return false;
		}
		
		$scope.submitFun=function(){
			
			if(!$scope.files.length){
				 Modal.alert("请选择要上传的文件");
				  return;
			}
			  for(var i = 0; i < $scope.files.length; i++){
				  var file = $scope.files[i];
				  var fileName = file.name;
				  var fileNameArray = fileName.split(".");
				  var fileType = fileNameArray[fileNameArray.length - 1];
				  
				  if(!isBelongToFileTypes(fileTypes, fileType)){
					  Modal.alert("文件格式只支持(xls),请将Excel文件保存为97-2003工作簿格式");
					  return;
				  }
				  if(file.size > 2097152){
					  Modal.alert(fileName + "的大小不能超过2M！");
					  return;
				  }
			  }
			
        
         var url=baseUrl + '/hcp/hRcp/contact/uploadDepartmentExcel.do';
         $upload.upload({
  				url : url,
  				params : {
  					providerid:$scope.institutionDetail.providerCode
				},
  				file : $scope.files
  			}).success(function(response) {
  			
  				$scope.$parent.queryTableData();
  				
  				$scope.onClose();
  				Modal.alert(response.message);
  				
  				
			}).error(function(r){
			})
		}
	
		 

		
		
		//关闭model
		$scope.onClose = function() {
			$modalInstance.close();
		}

		
	}]);
})();